package com.epalmpay.service.management.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.epalmpay.entity.Menu;
import com.epalmpay.entity.Role;
import com.epalmpay.mapper.MenuMapper;
import com.epalmpay.mapper.RoleMapper;
import com.epalmpay.service.management.IMenuService;
import com.epalmpay.util.RightsHelper;
import com.epalmpay.util.Tools;

@Service("menuService") 
public class MenuServiceImpl implements IMenuService {
	@Resource  
    private MenuMapper menuMapper;
	@Resource  
    private RoleMapper roleMapper;

	/**
	 * 通过ID获取其子一级菜单
	 */
	public List<Menu> listSubMenuByParentId(String parentId,Integer menuFlag) throws Exception {
		// TODO Auto-generated method stub
		return menuMapper.listSubMenuByParentId(parentId,menuFlag); 
	}
	
	
	/**
	 * 通过ID获取其子一级菜单
	 */
	public List<Menu> listSubMenuByModelParentId(String parentId,Integer menuFlag,Long groupId,Integer menuState) throws Exception {
		// TODO Auto-generated method stub
		return menuMapper.listSubMenuByModelParentId(parentId,menuFlag,groupId,menuState); 
	}

	/**
	 * 通过菜单ID获取数据
	 */
	public Menu getMenuById(Integer menuId) throws Exception {
		// TODO Auto-generated method stub
		return menuMapper.selectByPrimaryKey(menuId);
	}

	/**
	 * 新增菜单
	 * @param menu
	 * @throws Exception
	 */
	public void saveMenu(Menu menu) throws Exception {
		menuMapper.insert(menu);
	}

	/**
	 * 取最大ID
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public Menu findMaxId() throws Exception {
		return menuMapper.findMaxId();
	}

	/**
	 * 删除菜单
	 * @param MENU_ID
	 * @throws Exception
	 */
	public void deleteMenuById(Integer menuId) throws Exception {
		menuMapper.deleteByPrimaryKey(menuId);
		
	}

	/**
	 * 编辑
	 * @param menu
	 * @return
	 * @throws Exception
	 */
	public void edit(Menu menu) throws Exception {
		menuMapper.updateByPrimaryKey(menu);
		
	}

	/**
	 * 保存菜单图标 
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public void editicon(Menu menu) throws Exception {
		menuMapper.editicon(menu);
	}

	/**
	 * 获取所有菜单并填充每个菜单的子菜单列表(菜单管理)(递归处理)
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	public List<Menu> listAllMenu(String MENU_ID,Integer menuFlag) throws Exception {
		List<Menu> menuList = this.listSubMenuByParentId(MENU_ID,menuFlag);
		for(Menu menu : menuList){
			menu.setMenuUrl("menu/toEdit.do?MENU_ID="+menu.getMenuId()+"&&menuFlag="+menu.getMenuFlag());
			menu.setSubMenu(this.listAllMenu(menu.getMenuId()+"",menuFlag));
			menu.setTarget("treeFrame");
		}
		return menuList;
	}

	/**
	 * 获取所有菜单并填充每个菜单的子菜单列表(系统菜单列表)(递归处理)
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	public List<Menu> listAllMenuQx(String MENU_ID, Integer menuFlag) throws Exception {
		List<Menu> menuList = this.listSubMenuByParentId(MENU_ID,menuFlag);//通过ID获取其子一级菜单
		for(Menu menu : menuList){
			menu.setSubMenu(this.listAllMenuQx(menu.getMenuId()+"",menuFlag));
			menu.setTarget("treeFrame");
		}
		return menuList;
	}

	@Override
	/**
	 * menuId 顶级菜单
	 * menuFlag 1表示 掌富运营 2 渠道管理员3，品牌大商户
	 * groupId 商户id
	 */
	public List<Menu> listAllModelMenu(String menuId, Integer menuFlag, Long groupId,Integer menuState,String parentId) throws Exception {
		List<Menu> menuList = this.listSubMenuByModelParentId(menuId,menuFlag,groupId,menuState);//通过ID获取其子一级菜单
//		if(Tools.notEmpty(parentId)&&parentId.equals("0")&&menuList.size()==0){  
////			return null;
//		}
		for(Menu menu : menuList){
			List<Menu> subordinatemenuList=this.listAllModelMenu(menu.getMenuId()+"",menuFlag,groupId,null,menu.getParentId());
			menu.setSubMenu(subordinatemenuList);
			menu.setTarget("treeFrame");
		}
		return menuList;
	}

	
    
	@Override
	/**
	 * 根据用户id以及菜单id查询操作员是否拥有菜单权限 
	 */
	public boolean MenuRights(Long userId, Integer menuId) throws Exception {
		Role role=roleMapper.findObjectByUserId(userId);
		if(role!=null&&Tools.notEmpty(role.getRights())){
			//根据用户id查询角色信息
			boolean flag=RightsHelper.testRights(role.getRights(), menuId);
			return flag;
		}
		return false;
	}
}
